var Login = function () {

    var handleLogin = function () {
        $('.login-form').validate({
            errorElement: 'span', //default input error message container
            errorClass: 'help-block', // default input error message class
            focusInvalid: false, // do not focus the last invalid input
            rules: {
                username: {
                    required: true
                },
                password: {
                    required: true
                },
                captcha: {
                    required: true,
                    remote: {
                        url: "verifyCaptcha",
                        type: "post",
                        data: {
                            validateCode: function () {
                                return $("#captcha").val();
                            }
                            //必须要给他们定义方法；不能直接传至那样就是个死数；到时间触发不会发生变化；定义方法就会走方法重新获取走逻辑
                        }
                    }
                },
                remember: {
                    required: false
                }
            },

            messages: {
                username: {
                    required: "请输入账号！"
                },
                captcha: {
                    required: "请输入验证码！",
                    remote: "验证码错误!"
                },
                password: {
                    required: "请输入密码！"
                }
            },

            invalidHandler: function (event, validator) { //display error alert on form submit
                $('.alert-danger', $('.login-form')).show();
            },

            highlight: function (element) { // hightlight error inputs
                $(element)
                    .closest('.form-group').addClass('has-error'); // set error class to the control group
            },

            success: function (label) {
                label.closest('.form-group').removeClass('has-error');
                label.remove();
            },

            errorPlacement: function (error, element) {
                error.insertAfter(element.closest('.input-icon'));
            },

            submitHandler: function (form) {
                form.submit(); // form validation success, call ajax form submit
            }
        });

        $('.login-form input').keypress(function (e) {
            if (e.which == 13) {
                var login_form = $('.login-form');
                if (login_form.validate().form()) {
                    login_form.submit(); //form validation success, call ajax form submit
                }
                return false;
            }
        });
        $("#username").bind("keydown", function (e) {
            if (e.which == 13) {
                e.preventDefault();
                $("#password").focus();
            }
        });
        $("#password").bind("keydown", function (e) {
            if (e.which == 13) {
                e.preventDefault();
                $("#captcha").focus();
            }
        });

        $("#captchaImage,#captchaTitle").click(function () {
            $("#captchaImage").attr("src", "captcha?" + Math.random());
        });

        jQuery('#login-back-btn').click(function () {
            window.location.href = "/scorm/";
        });

        jQuery('#forget-password').click(function () {
            jQuery('.login-form').hide();
            jQuery('.forget-form').show();
        });

        jQuery('#back-btn').click(function () {
            jQuery('.forget-form').hide();
            jQuery('.login-form').show();
        });
    };

    var handleForget = function () {
        $('.forget-form').validate({
            errorElement: 'span', //default input error message container
            errorClass: 'help-block', // default input error message class
            focusInvalid: false, // do not focus the last invalid input
            rules: {
                loginName: {
                    required: true,
                    remote: {
                        url: "public/checkRepeatLoginName",
                        type: "post",
                        data: {
                            loginName: function () {
                                return $("#loginName").val();
                            },
                            forgetPassword: function () {
                                return true;
                            },
                            //必须要给他们定义方法；不能直接传至那样就是个死数；到时间触发不会发生变化；定义方法就会走方法重新获取走逻辑
                            oldName: function () {
                                return "";
                            }
                        }
                    }
                },
                question: {
                    required: true
                }
            },

            messages: {
                loginName: {
                    required: "账号不能为空！",
                    remote: "账号不存在!"
                },

                question: {
                    required: "请输入确认信息！"
                }
            },

            invalidHandler: function (event, validator) { //display error alert on form submit
                $('.alert-danger', $('.login-form')).show();
            },

            highlight: function (element) { // hightlight error inputs
                $(element)
                    .closest('.form-group').addClass('has-error'); // set error class to the control group
            },

            success: function (label) {
                label.closest('.form-group').removeClass('has-error');
                label.remove();
            },

            errorPlacement: function (error, element) {
                error.insertAfter(element.closest('.input-icon'));
            },

            submitHandler: function (form) {
                form.submit(); // form validation success, call ajax form submit
            }
        });

        $('.forget-form input').keypress(function (e) {
            if (e.which == 13) {
                var forget_form = $('.forget-form');
                if (forget_form.validate().form()) {
                    forget_form.submit(); //form validation success, call ajax form submit
                }
                return false;
            }
        });
    };

    return {
        //main function to initiate the module
        init: function () {
            handleLogin();
            handleForget();
            $.backstretch([
                "img/login/bg/02.jpg",
//                "img/login/bg/01.jpg",
//                "img/login/bg/03.jpg",
                "img/login/bg/04.jpg",
                "img/login/bg/05.jpg"
            ], {
                fade: 1000,
                duration: 5000
            });
        }

    };

}();